<template>
  <div class="Background-image">
    <el-form ref="LoginUserInfo" class="userLogin" :model="userinfo" :rules="rules">
      <div class="line">
        <div class="line1"></div>
        <div class="line2"></div>
        <div class="left"></div>
        <div class="right"></div>
      </div>
      <h4>橙子商城</h4>
      <el-form-item lable="用户名:" prop="username" class="userName">
        <div class="div-el-icon">
          <el-icon :size="26" style="color: rgb(147, 140, 140)">
            <User />
          </el-icon>
        </div>
        <el-input placeholder="请输入用户名" v-model="userinfo.username" class="userName_input" />
      </el-form-item>

      <el-form-item lable="密码:" prop="password" class="pwdName">
        <div class="div-el-icon">
          <el-icon :size="26" style="color: rgb(147, 140, 140)">
            <Lock />
          </el-icon>
        </div>
        <el-input type="password" placeholder="请输入密码" v-model="userinfo.password" class="pwd_input" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit(LoginUserInfo)" class="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts" name="LoginPage">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { LoginApi } from '../../api/index.js'
import { ElMessage } from 'element-plus'
const userinfo = ref({
  username: '',
  password: ''
})
const rules = {
  username: [
    {
      required: true,
      message: '该信息必填'
    },
    {
      pattern: /^\w{4,8}$/,
      message: '用户名为4-8位字母数字下划线组成'
    }
  ],
  password: [
    {
      required: true,
      message: '密码必填'
    },
    {
      pattern: /^\w{4,8}$/,
      message: '密码为4-8位字母数字下划线组成'
    }
  ],
}

const LoginUserInfo = ref(null)

const router = useRouter()
const onSubmit = (LoginUserInfo) => {
  LoginUserInfo.validate().then(() => {
    LoginApi({
      username: userinfo.value.username,
      password: userinfo.value.password
    }).then((res) => {
      console.log(res);

      if (res.data.code == 1) {
        localStorage.setItem('token', res.data.token)
        ElMessage({
          type: 'success',
          message: '登录成功',
          onClose: () => {
            router.replace('/dashboard')
          }
        })
      }
    })
  })
}

</script>

<style scoped lang="scss">
.Background-image {
  width: 100vw;
  height: 100vh;
  background-image: url(https://bpic.588ku.com/back_pic/05/95/06/725d46f469760ce.jpg);
  background-size: 100% 100%;

  .userLogin {
    width: 330px;
    height: 370px;
    background: white;
    position: fixed;
    right: 120px;
    opacity: 1;
    top: 95px;
    padding: 20px 30px;
    box-sizing: border-box;

    .userName {
      margin-top: 70px;
      display: flex;
      justify-content: space-between;

      .userName_input {
        height: 40px;
        width: 230px;
        margin-left: 10px;
      }
    }

    .pwdName {
      .pwd_input {
        height: 40px;
        width: 230px;
        margin-left: 10px;
      }
    }

    .login {
      margin-top: 10px;
      width: 330px;
      height: 40px;
      border-radius: 10px;
    }

    .yanzhenma {
      height: 40px;
      width: 141px;
      margin-left: 10px;
    }
  }
}

.div-el-icon {
  padding-top: 10px;
  box-sizing: border-box;
}

.line {
  display: flex;
  margin-bottom: 40px;
  position: relative;

  .line1 {
    position: absolute;
    height: 100px;
    width: 2px;
    background-color: rgb(53, 165, 244);
    top: -100px;
    left: 23px;
  }

  .line2 {
    position: absolute;
    height: 100px;
    width: 2px;
    background-color: rgb(41, 159, 243);
    top: -100px;
    left: 249px;
  }

  .left {
    width: 18px;
    height: 18px;
    background-color: rgb(41, 165, 254);
    border-radius: 50%;
    border: 1px solid rgb(81, 152, 239);
    box-sizing: border-box;
    margin-left: 15px;
  }

  .right {
    width: 18px;
    height: 18px;
    background-color: rgb(41, 165, 254);
    border-radius: 50%;
    border: 1px solid rgb(81, 152, 239);
    box-sizing: border-box;
    margin-left: 208px;
  }
}

h4 {
  position: absolute;
  top: 50px;
  left: 115px;
  font-size: 24px;
  color: rgb(81, 152, 239);
  text-align: center;
}
</style>
